package cn.kgc.vue.controller;

import cn.kgc.vue.annotation.LogAnnotation;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Course;
import cn.kgc.vue.entity.Schedule;
import cn.kgc.vue.entity.Student;
import cn.kgc.vue.service.CourseService;
import cn.kgc.vue.service.ScheduleService;
import cn.kgc.vue.service.StudentService;
import cn.kgc.vue.enums.OperationType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("student")
@Api(tags = "学生模块")
public class StudentController {

    @Resource
    private CourseService courseService;
    @Resource
    private StudentService studentService;
    @Resource
    private ScheduleService scheduleService;

    @ApiOperation(value = "学生分页")
    @PostMapping("list")
    public ResponseResult page(@RequestBody QueryCondition<Student> student) {
       return studentService.studentPage(student);
    }

    @ApiOperation(value = "下拉框")
    @GetMapping("getCourse")
    public ResponseResult getCourse() {

        List<Course> list = courseService.selectAllIncludingDeleted();
        List<Schedule> list1 = scheduleService.selectAllIncludingDeleted();
        return ResponseResult.success().data("courseList",list).data("clazzList",list1);
    }

    @LogAnnotation(model="学生模块",operation= OperationType.ADD)
    @ApiOperation(value = "新增和修改")
    @PostMapping("saveOrUpdateMessage")
    public ResponseResult saveOrUpdateMessage(@RequestBody Student student) {
        if (student.getId() == null) {
            boolean b = studentService.saveOrUpdate(student);
            if (!b){
                return ResponseResult.fail().message("请刷新后重试");
            }
            return ResponseResult.success().message("新增成功");
        }

        boolean b = studentService.saveOrUpdate(student);
        if(!b){
            return ResponseResult.fail().message("请刷新后重试");
        }
        return ResponseResult.success().message("编辑成功");
    }

    @LogAnnotation(model="学生模块",operation= OperationType.DELETE)
    @ApiOperation(value = "删除用户")
    @DeleteMapping("studentDelete/{id}")
    public ResponseResult studentDelete(@PathVariable Integer id) {
        studentService.removeById(id);
        return ResponseResult.success().message("删除成功");
    }

    @ApiOperation(value = "批量导入")
    @PostMapping("import")
    public ResponseResult importStudent( MultipartFile file) throws Exception {


        return studentService.importStudent(file);
    }

    @PostMapping("export")
    @ApiOperation("批量导出")
    public ResponseResult exportStudent(HttpServletResponse response, @RequestBody Map<String,Object> exportParam) throws Exception {
      return   studentService.exportStudent(response,exportParam);
    }

}
